import request from '@/utils/request'
import { requestWrapper, randomInt } from '@/utils/mock'

// 获取字典类型列表
export function getDictTypeList(query) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/type/list',
      method: 'get',
      params: query
    }),
    // 模拟数据
    () => {
      const { pageNum = 1, pageSize = 10, dictName, dictType, status } = query;
      const total = 3; // 模拟总数
      const list = [
        {
          dictId: 1,
          dictName: '性别',
          dictType: 'sys_user_gender',
          status: '1',
          remark: '用户性别字典',
          createTime: '2023-01-01 10:00:00'
        },
        {
          dictId: 2,
          dictName: '系统状态',
          dictType: 'sys_common_status',
          status: '1',
          remark: '通用状态字典',
          createTime: '2023-01-02 11:00:00'
        },
        {
          dictId: 3,
          dictName: '通知类型',
          dictType: 'sys_notice_type',
          status: '1',
          remark: '系统通知类型',
          createTime: '2023-01-03 12:00:00'
        }
      ].filter(item => {
        return (!dictName || item.dictName.includes(dictName)) &&
               (!dictType || item.dictType.includes(dictType)) &&
               (status === undefined || item.status === status);
      });

      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = Math.min(startIndex + pageSize, list.length);
      const paginatedList = list.slice(startIndex, endIndex);

      return {
        list: paginatedList,
        total: list.length,
        pageNum: parseInt(pageNum),
        pageSize: parseInt(pageSize)
      };
    }
  );
}

// 获取字典类型详情
export function getDictTypeDetail(dictId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/dict/type/${dictId}`,
      method: 'get'
    }),
    // 模拟数据
    () => {
      const mockDetails = {
        '1': {
          dictId: 1,
          dictName: '性别',
          dictType: 'sys_user_gender',
          status: '1',
          remark: '用户性别字典',
          createTime: '2023-01-01 10:00:00'
        },
        '2': {
          dictId: 2,
          dictName: '系统状态',
          dictType: 'sys_common_status',
          status: '1',
          remark: '通用状态字典',
          createTime: '2023-01-02 11:00:00'
        },
        '3': {
          dictId: 3,
          dictName: '通知类型',
          dictType: 'sys_notice_type',
          status: '1',
          remark: '系统通知类型',
          createTime: '2023-01-03 12:00:00'
        }
      };
      return mockDetails[dictId] || null;
    }
  );
}

// 新增字典类型
export function addDictType(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/type',
      method: 'post',
      data: data
    }),
    // 模拟数据
    {
      code: 200,
      msg: '新增成功',
      data: null
    }
  );
}

// 修改字典类型
export function updateDictType(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/type',
      method: 'put',
      data: data
    }),
    // 模拟数据
    {
      code: 200,
      msg: '修改成功',
      data: null
    }
  );
}

// 删除字典类型
export function deleteDictType(dictIds) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/dict/type/${dictIds}`,
      method: 'delete'
    }),
    // 模拟数据
    {
      code: 200,
      msg: '删除成功',
      data: null
    }
  );
}

// 获取字典数据列表
export function getDictDataList(dictType) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/data/type/' + dictType,
      method: 'get'
    }),
    // 模拟数据
    () => {
      const dataList = {
        'sys_user_gender': [
          { dictCode: 101, dictType: 'sys_user_gender', dictLabel: '男', dictValue: '0', cssClass: '', listClass: '', isDefault: true, status: '1', createTime: '2023-01-01 10:00:00' },
          { dictCode: 102, dictType: 'sys_user_gender', dictLabel: '女', dictValue: '1', cssClass: '', listClass: '', isDefault: false, status: '1', createTime: '2023-01-01 10:00:00' },
          { dictCode: 103, dictType: 'sys_user_gender', dictLabel: '未知', dictValue: '2', cssClass: '', listClass: '', isDefault: false, status: '1', createTime: '2023-01-01 10:00:00' }
        ],
        'sys_common_status': [
          { dictCode: 201, dictType: 'sys_common_status', dictLabel: '正常', dictValue: '1', cssClass: '', listClass: '', isDefault: true, status: '1', createTime: '2023-01-02 11:00:00' },
          { dictCode: 202, dictType: 'sys_common_status', dictLabel: '停用', dictValue: '0', cssClass: '', listClass: '', isDefault: false, status: '1', createTime: '2023-01-02 11:00:00' }
        ],
        'sys_notice_type': [
          { dictCode: 301, dictType: 'sys_notice_type', dictLabel: '通知', dictValue: '1', cssClass: '', listClass: '', isDefault: true, status: '1', createTime: '2023-01-03 12:00:00' },
          { dictCode: 302, dictType: 'sys_notice_type', dictLabel: '公告', dictValue: '2', cssClass: '', listClass: '', isDefault: false, status: '1', createTime: '2023-01-03 12:00:00' }
        ]
      };
      return dataList[dictType] || [];
    }
  );
}

// 获取字典数据详情
export function getDictDataDetail(dictCode) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/dict/data/${dictCode}`,
      method: 'get'
    }),
    // 模拟数据
    () => {
      const mockDataDetails = {
        '101': { dictCode: 101, dictType: 'sys_user_gender', dictLabel: '男', dictValue: '0', cssClass: '', listClass: '', isDefault: true, status: '1', createTime: '2023-01-01 10:00:00' },
        '102': { dictCode: 102, dictType: 'sys_user_gender', dictLabel: '女', dictValue: '1', cssClass: '', listClass: '', isDefault: false, status: '1', createTime: '2023-01-01 10:00:00' },
        '103': { dictCode: 103, dictType: 'sys_user_gender', dictLabel: '未知', dictValue: '2', cssClass: '', listClass: '', isDefault: false, status: '1', createTime: '2023-01-01 10:00:00' },
        '201': { dictCode: 201, dictType: 'sys_common_status', dictLabel: '正常', dictValue: '1', cssClass: '', listClass: '', isDefault: true, status: '1', createTime: '2023-01-02 11:00:00' },
        '202': { dictCode: 202, dictType: 'sys_common_status', dictLabel: '停用', dictValue: '0', cssClass: '', listClass: '', isDefault: false, status: '1', createTime: '2023-01-02 11:00:00' },
        '301': { dictCode: 301, dictType: 'sys_notice_type', dictLabel: '通知', dictValue: '1', cssClass: '', listClass: '', isDefault: true, status: '1', createTime: '2023-01-03 12:00:00' },
        '302': { dictCode: 302, dictType: 'sys_notice_type', dictLabel: '公告', dictValue: '2', cssClass: '', listClass: '', isDefault: false, status: '1', createTime: '2023-01-03 12:00:00' }
      };
      return mockDataDetails[dictCode] || null;
    }
  );
}

// 新增字典数据
export function addDictData(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/data',
      method: 'post',
      data: data
    }),
    // 模拟数据
    {
      code: 200,
      msg: '新增成功',
      data: null
    }
  );
}

// 修改字典数据
export function updateDictData(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/data',
      method: 'put',
      data: data
    }),
    // 模拟数据
    {
      code: 200,
      msg: '修改成功',
      data: null
    }
  );
}

// 删除字典数据
export function deleteDictData(dictCodes) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/dict/data/${dictCodes}`,
      method: 'delete'
    }),
    // 模拟数据
    {
      code: 200,
      msg: '删除成功',
      data: null
    }
  );
} 